package com.heima.user.service.impl;

import com.heima.common.dtos.ResponseResult;
import com.heima.common.threadlocal.UserThreadLocalUtils;
import com.heima.feign.ApArticleFeignClient;
import com.heima.model.article.dtos.ArticleDto;
import com.heima.model.article.dtos.AuthorDto;
import com.heima.model.user.dtos.ApUserFollowDto;
import com.heima.user.entity.ApUser;
import com.heima.user.entity.ApUserFan;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.heima.user.entity.ApUserFollow;
import com.heima.user.mapper.ApUserFollowMapper;
import com.heima.user.service.ApUserFanService;
import com.heima.user.service.ApUserFollowService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import com.heima.user.service.ApUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * APP用户关注信息表 服务实现类
 * </p>
 *
 * @author HM
 * @since 2023-03-22
 */
@Service
public class ApUserFollowServiceImpl extends ServiceImpl<ApUserFollowMapper, ApUserFollow> implements ApUserFollowService {

    @Autowired
    private ApArticleFeignClient articleFeignClient;
    @Autowired
    private ApUserService apUserService;
    @Autowired
    private ApUserFanService apUserFanService;
    @Autowired
    private ApUserFollowService apUserFollowService;

    /**
     * 关注与取消关注
     * @param dto
     * @return
     */
    @Override
    public ResponseResult userFollow(ApUserFollowDto dto) {
        //根据articleId查询文章
        ArticleDto articleDto = articleFeignClient.findById(dto.getArticleId());
        //查询authorId查询文章作者信息
        AuthorDto authorDto = articleFeignClient.authorDtoFindById(articleDto.getAuthorId());
        //获取到社交账号id
        ApUser apUser = apUserService.getById(authorDto.getUserId());
        //判断是否关注
        if(dto.getOperation() == 0){
            //关注
            //封装粉丝数据
            ApUser localApUser = apUserService.getById(1);
            ApUserFan apUserFan = new ApUserFan();
            apUserFan.setUserId(apUser.getId());
            apUserFan.setFansId(localApUser.getId());
            apUserFan.setFansName(localApUser.getName());
            apUserFan.setCreatedTime(new Date());
            apUserFan.setIsDisplay(true);
            apUserFanService.save(apUserFan);


            //封装app用户关注信息表
            ApUserFollow apUserFollow = new ApUserFollow();
            apUserFollow.setUserId(localApUser.getId());
            apUserFollow.setFollowId(authorDto.getUserId());
            apUserFollow.setFollowName(authorDto.getName());
            apUserFollow.setCreatedTime(new Date());
            apUserFollowService.save(apUserFollow);
        }
        return ResponseResult.ok();
    }
    @Override
    public ApUserFollow findByUserIdAndFollowId(Integer userId, Integer followId) {
        QueryWrapper<ApUserFollow> queryWrapper=new QueryWrapper<>();
        queryWrapper.lambda().eq(ApUserFollow::getUserId,userId);
        queryWrapper.lambda().eq(ApUserFollow::getFollowId,followId);
        return this.getOne(queryWrapper);

    }
}
